#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import xml.dom.minidom
import tkinter as tk
from tkinter import ttk
import socket
import json

#open xml file
dom = xml.dom.minidom.parse('UNData.xml')

#get elements in the file
root = dom.documentElement

#search by Tag
cc = dom.getElementsByTagName('Country')
country = []
for c in cc:
    # elements of country
    
    a = 0
    while a < len(cc):
        country.append(cc[a].firstChild.data)
        a+=28

yy = dom.getElementsByTagName('Year')
year = []
for y in yy:
    # elements of year
    
    b = 0
    while b < len(yy):
        year.append(yy[b].firstChild.data)
        b+=1

vv = dom.getElementsByTagName('Value')
value = []
for v in vv:
    # elements of value
    
    d = 0
    while d < len(vv):
        value.append(vv[d].firstChild.data)
        d+=1
country_info = {}
for i in range(len(country)):
    country_info[country[i]] = {'year':year[i*28:(i+1)*28],'value':value[i*28:(i+1)*28]}
ip_port = (socket.gethostname(), 50000)
sk = socket.socket()            # create a socket
sk.bind(ip_port)                # bind host address and port together
sk.listen(5)                    # # configure how many client the server can listen simultaneously
print('Starting socket，waiting for connection...')
flag = True
while True:
    conn, address = sk.accept()# # accept new connection
    # receive data stream. it won't accept data packet greater than 1024 bytes
    while True:
        client_data = conn.recv(1024).decode()
        client_data = str(client_data)
        dataStr = country_info[client_data]
        dataStr = json.dumps(dataStr)
        print("Message from Client %s：%s" % (address, client_data))
        conn.send(dataStr.encode()) # # send data to the client
        break
    conn.close()


# In[ ]:




